// <copyright file="WSTrustFaultException.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-20</date>
// <summary>A base class for WSFault exceptions.</summary>

namespace SharpSTS
{
    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    
    /// <summary>
    /// A base class for WSFault exceptions.
    /// </summary>
    [Serializable]
    public abstract class WSTrustFaultException : FaultException
    {       
        /// <summary>
        /// Initializes a new instance of the <see cref="WSTrustFaultException"/> class.
        /// </summary>
        /// <param name="reason">The fault reason.</param>
        /// <param name="code">The fault code.</param>
        protected WSTrustFaultException(FaultReason reason, FaultCode code)
            : base(reason, code)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WSTrustFaultException"/> class.
        /// </summary>
        /// <param name="reason">The fault reason.</param>
        /// <param name="code">The fault code.</param>
        /// <param name="subCode">The fault subcode.</param>
        protected WSTrustFaultException(FaultReason reason, FaultCode code, FaultCode subCode)
            : base(reason, new FaultCode(code.Name, Constants.WSTrust.Namespace.Uri, new FaultCode(subCode.Name, subCode.Namespace)))
        {            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WSTrustFaultException"/> class.
        /// </summary>
        /// <param name="serializationInfo">The serialization information.</param>
        /// <param name="streamingContext">The streaming context.</param>
        protected WSTrustFaultException(SerializationInfo serializationInfo, StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {            
        }

        /// <summary>
        /// Builds a WSTrust fault exception using the information in the specified exception.
        /// </summary>
        /// <param name="ex">The exception to wrap in a <see cref="WSTrustFaultException"/>.</param>
        /// <returns>A <see cref="WSTrustFaultException"/> wrapping the specified exception.</returns>
        public static WSTrustFaultException FromException(Exception ex)
        {
            WSTrustFaultException faultException = ex as WSTrustFaultException ?? new RequestFailedFaultException();

            return faultException;
        }
    }
}